<?php

/**
 * Created by PhpStorm.
 * User: DELL
 * Date: 2017/6/10
 * Time: 17:33
 */
require_once 'BasePriv.php';
//class Test extends CI_Controller
class Test extends BasePriv
{
    public function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
    }

    public function index()
    {
//        $this->load->database();
//        $db = $this->db;
//        $query = $db->query("select * from jper_video_list where is_publish=2 and is_online=2");
//        $result = $query->result_array();
//        foreach($result as $key=>$val){
//            $db->insert("jper_hot_video",[
////                "is_publish"=>$val["is_publish"],
////                "online_date"=>date("Y-m-d",$val["add_time"]),
////                "online_time"=>date("Y-m-d H:i:s",$val["add_time"]),
//                "order"=>$val["weight"],
//                "add_time"=>date("Y-m-d H:i:s"),
//                "video_id"=>$val["video_id"]
//            ]);
//        }
//        echo "<pre>";
//        print_r($_SERVER);
////        echo base_url();
//        echo "http://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }
    public function prepare()
    {
//        $this->load->database();
//        $this->load->helper("common");
//        $videoIds = [267710699277582337];
//        $videoIds = implode(",",$videoIds);
//        $this->db->from("video_list");
//        $this->db->where("video_id in($videoIds)");
//        $query = $this->db->get();
//        $videos = $query->result_array();
//        foreach($videos as $video){
//            $fileAddr = getFileAddr($video["video_url"]);
//            $filename = md5(microtime().rand(1,1000)).".jpg";
//            $this->config->load("upload");
//
//            $upload_config = config_item("upload")["cover"];
//            $savePath = $upload_config["uploadpath"].$filename;
//            $coverHttp = $upload_config["savehttp"].$filename;
//
//            $status = getImgFromVideo($fileAddr,$savePath);
//            $this->db->update("video_list",["cover"=>$coverHttp],["video_id"=>$video["video_id"]]);
//        }

    }
    public function benchmark()
    {
//        $start = time();
//        $pdo = new Pdo("mysql:dbname=jper;host=172.31.32.27;port=3306","jper",'!QA@WS#ED$RF');
//        $sql = "insert into jper_test(user_id,name)values(?,?)";
//        $statement = $pdo->prepare($sql);
////        for($i=0;$i<10000;$i++){
//            $statement->execute($sql,[$i,"dxp_".$i]);
////        }
//        $end = time();
//        echo $end-$start;
    }
    public function activity()
    {
//        $aid = 261638626508963841;
//        $videoIds = [
//            267710699277582337
//        ];
//        $videoIds = implode(",",$videoIds);
//        $this->load->database();
//        $query = $this->db->from("video_list")->where("video_id in($videoIds)")->get();
//        $videos = $query->result_array();
//        foreach($videos as $video){
//            $data = [
//                "user_id"=>$video["user_id"],
//                "aid"=>$aid,
//                "add_time"=>date("Y-m-d H:i:s"),
//                "video_id"=>$video["video_id"]
//            ];
//            $this->db->insert("activity_video",$data);
//            echo '插入===>'.$video["old_title"]."<br>";
//        }
    }
    public function videoTag()
    {
        $this->load->database();
        $this->db->select("*");
        $this->db->from("video_list");
        $query = $this->db->get();
        $videos = $query->result_array();
        foreach($videos as $video){
            if(!empty($video["tag_id"])){
                $this->db->from("video_tag");
                $this->db->where(["video_id"=>$video["video_id"],"tag_id"=>$video["tag_id"]]);
                $nums = $this->db->count_all_results();
                if($nums == 0 ){
                    $result = $this->db->insert("video_tag",[
                        "video_id"=>$video["video_id"],
                        "tag_id"=>$video["tag_id"],
                        "add_time"=>$video["add_time"]
                    ]);
                    if($result !== FALSE){
                        echo "video_id:{$video["video_id"]},tag_id:{$video["tag_id"]} 成功";
                    }else{
                        echo "video_id:{$video["video_id"]},tag_id:{$video["tag_id"]} 失败";
                    }
                }
            }
        }

    }
    public function addChannel()
    {
        $this->load->database();
        $channels = [
            ["guanwang","官网"],["_360","_360"],["baidu","百度"],
            ["wandoujia","豌豆荚"],["vivo","vivo"],["xiaomi","小米"],
            ["oppo","oppo"],["huawei","华为"],["meizu","魅族"]
        ];
        $start = 7;
        foreach($channels as $channel){

            $this->db->insert("channel",[
                "channel_id"=>$start,
                "short_name"=>$channel[0],
                "channel_name"=>$channel[1],
                "type"=>2
            ]);
            $start++;
        }
    }
    public function recover()
    {
        $db1 = $this->load->database("cock_zs",true);

//        $db2 = $this->load->database("cock",true);

//        $result_cock = $db2->select("video_id,video_url,del_video_url,del_time,status,status1,admin_id")->from("video_list")->where(["user_id"=>253517488921739265])->get()->result_array();
//        foreach($result_cock as $video){
//            $videoId = $video["video_id"];
//            unset($video["video_id"]);
//            echo $videoId."<br>";
//            $result = $db1->update("video_list",$video,["video_id"=>$videoId]);
//            var_dump($result);
//        }
//        echo "<pre>";
//        print_r($result_cock);
//        $db1->select("a.video_id,a.id");
//        $db1->from("prepare_video as a");
//        $db1->join("video_list as b","a.video_id=b.video_id");
//        $db1->where(["b.is_secret"=>2]);
//        $query = $db1->get();
//        $result = $query->result_array();
//        if(count($result) > 20){
//            exit("错误的查询");
//        }
//        foreach($result as $video){
//            $db1->update("video_list",["status"=>0,"status1"=>-1],["video_id"=>$video["video_id"]]);
//            $db1->delete("prepare_video",["id"=>$video["id"]]);
//
//            echo "video_id:{$video["video_id"]}"."==== id:{$video["id"]}";
//        }

    }
    public function move_lib()
    {
        $this->load->database();

        $this->db->from("video_list");
        $count = $this->db->count_all_results();

        $page = ceil($count / 100);
        for($i=1;$i<=$page;$i++){
            $sql = "select video_id,video_url,cover from jper_video_list order by video_id asc limit 100 offset ".($i-1)*100;
            $query = $this->db->query($sql);
            $result = $query->result_array();
            foreach ($result as &$row){
//                $videoId = $row["video_id"];
//                $new_cover = str_replace("cdn-","cdn1-",$row["cover"]);
//                $new_video_url = str_replace("cdn-","cdn1-",$row["video_url"]);
//                $is_success = $this->db->update("video_list",[
//                    "video_url"=>$new_video_url,
//                    "cover"=>$new_cover,
//                ],["video_id"=>$videoId]);
//                if($is_success !== FALSE){
//                    echo "修改成功====".$videoId."==cover=={$new_cover}"."<br>";
//                }else{
//                    echo "修改失败====".$videoId."<br>";
//                    break 2;
//                }
            }
            unset($result);
        }
    }
    public  function activity_set()
    {
        $this->load->database();
        $sql = "select * from jper_activities ";
        $query = $this->db->query($sql);
        $result = $query->result_array();
        foreach ($result as &$row){

//            $activity_image = str_replace("cdn-","cdn1-",$row["activity_image"]);
//            $activity_icon = str_replace("cdn-","cdn1-",$row["activity_icon"]);
//
//            $is_success = $this->db->update("activities",[
//                    "activity_image"=>$activity_image,
//                    "activity_icon"=>$activity_icon,
//                ],["aid"=>$row["aid"]]);
//                if($is_success !== FALSE){
//                    echo "修改成功====".$row["aid"]."==image=={$activity_image}"."<br>";
//                }else{
//                    echo "修改失败====".$row["aid"]."<br>";
//                    break ;
//                }
        }
    }
    public  function advertise()
    {
        $this->load->database();

        $sql = "select * from jper_enter_advertisement ";
        $query = $this->db->query($sql);
        $result = $query->result_array();
//        foreach ($result as &$row){
//            $advertisement_image = str_replace("cdn-","cdn1-",$row["advertisement_image"]);
//            $advertisement_video = str_replace("cdn-","cdn1-",$row["advertisement_video"]);
//            $is_success = $this->db->update("enter_advertisement",[
//                "advertisement_image"=>$advertisement_image,
//                "advertisement_video"=>$advertisement_video,
//            ],["advertisement_id"=>$row["advertisement_id"]]);
//            if($is_success !== FALSE){
//                echo "修改成功====".$row["advertisement_id"]."==image=={$advertisement_image}"."<br>";
//            }else{
//                echo "修改失败====".$row["advertisement_id"]."<br>";
//                break ;
//            }
//        }
    }
    public function set_ma()
    {
        $this->load->database();

        $sql1 = "select * from jper_material_pattern";
        $sql2 = "select * from jper_material_audio";
        $sql3 = "select * from jper_material_sound";

        $query = $this->db->query($sql1);
        $result1 = $query->result_array();

        foreach ($result1 as &$row){
            $row["resource"] = str_replace("cdn-","cdn1-",$row["resource"]);
            $this->db->update("material_pattern",[
                "resource"=>$row["resource"],
            ],["mid"=>$row["mid"]]);
        }

        $query = $this->db->query($sql2);
        $result2 = $query->result_array();

        foreach ($result2 as &$row){
            $row["resource"] = str_replace("cdn-","cdn1-",$row["resource"]);
            $this->db->update("material_audio",[
                "resource"=>$row["resource"],
            ],["mid"=>$row["mid"]]);
        }

        $query = $this->db->query($sql3);
        $result3 = $query->result_array();

        foreach ($result3 as &$row){
            $row["resource"] = str_replace("cdn-","cdn1-",$row["resource"]);
            $this->db->update("material_sound",[
                "resource"=>$row["resource"],
            ],["mid"=>$row["mid"]]);
        }



    }
    public function ffmpeg()
    {
        $coverAddr = "/data0/dk/jpercdn/jper/app/cover/2017/09/21/";
        $coverHttp = "http://cdn1-jper.foundao.com/jper/app/";
        $this->load->database();
        $time = 1505923200;
        $sql = "select * from jper_video_list where add_time > {$time} ";
        $query = $this->db->query($sql);
        $result = $query->result_array();
        echo "<pre>";
        print_r($result);
//        foreach ($result as &$row){
//            $filename = md5(time().rand(1,10000)).".png";
//            $video_addr = $this->getAddr($row["video_url"]);
//            if($this->getImgFromVideo($video_addr,$coverAddr.$filename)){
//                $is_success = $this->db->update("video_list",["cover"=>$coverHttp."cover/2017/09/21/".$filename],["video_id"=>$row["video_id"]]);
//                if($is_success !== false){
//                    echo "成功..."."<br>";
//                }
//            }
//        }
    }
    public function set_mat()
    {

    }
    private  function getImgFromVideo($video, $img, $time = 0) {
        echo "video==>".$video."<br>";
        echo "img==>".$img."<br>";
        $dirName = dirname($img);
        if (!file_exists($dirName)) {
            exec("mkdir -p {$dirName}");
        }
        $com = "/app/ffmpeg/ffmpeg  -i $video -y -f  image2  -ss {$time} -vframes 1  {$img}";
        passthru($com, $status);
        if ($status == 0 && file_exists($img)) {
            return true;
        } else {
            return false;
        }
    }
    public function getAddr($cdnUrl){
        echo "cdn-url:".$cdnUrl."<br>";
        $baseUrl = "http://cdn1-jper.foundao.com/jper/app";
        $len = strlen($baseUrl);
        $suffix = substr($cdnUrl,$len+1);
        $prefixPath = "/data0/dk/jpercdn/jper/app/";
        $path = $prefixPath.$suffix;
        return $path;
    }

    public function test(){
//        $this->redis->setex("jony_j",60,147258);
        $res=$this->redis->get("jony_j");
        var_dump($res);
    }

}